package com.example.book.in.controller;

import com.example.book.base.BaseController;
import com.example.book.base.Result;
import com.example.book.in.entity.BookInfo;
import com.example.book.in.service.BookInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import com.example.book.in.entity.BookCatalog;
import com.example.book.in.service.BookCatalogService;


/**
 * @author hcj
 * @date 2019-10-27 22:24:09
 */
@Controller
@RequestMapping(value = "/sys/in/bookCatalog")
public class BookCatalogController extends BaseController {

    @Autowired
    private BookCatalogService bookCatalogService;
    @Autowired
    private BookInfoService bookInfoService;

    @ModelAttribute
    public BookCatalog get(@RequestParam(required = false) String id) {
        if (StringUtils.isNotBlank(id)) {
            BookCatalog bookCatalog = new BookCatalog();
            bookCatalog.setId(id);
            return bookCatalogService.get(bookCatalog);
        } else {
            return new BookCatalog();
        }
    }

    @RequestMapping(value = "/get")
    @ResponseBody
    public BookCatalog getById(BookCatalog bookCatalog) {
        return bookCatalog;
    }


    /**
     * 新增修改页面
     *
     * @param catalog
     * @param model
     * @return
     */
    @GetMapping("/form")
    public String form(BookCatalog catalog, String bookId, Model model) {
        if (catalog != null) {
            model.addAttribute("catalog", catalog);
        }
        BookInfo info = new BookInfo();
        info.setId(bookId);
        BookInfo bi = bookInfoService.get(info);

        model.addAttribute("bookId", bookId);
        model.addAttribute("bookName", bi.getBookName());

        return "in/catalog/catalog_form";
    }

    /**
     * 书籍目录列表页面
     *
     * @param bookCatalog
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(BookCatalog bookCatalog, String bookId, Model model) {
        model.addAttribute("bookId", bookId);
        return "in/catalog/catalog_list";
    }

    @RequestMapping(value = "/info")
    @ResponseBody
    public List<BookCatalog> info(BookCatalog bookCatalog) {
        return bookCatalogService.findList(bookCatalog);
    }


    @RequestMapping(value = "/findPage")
    @ResponseBody
    public PageInfo<BookCatalog> findPage(BookCatalog bookCatalog) {
        PageInfo<BookCatalog> pageInfo = bookCatalogService.findPage(bookCatalog);
        BookInfo info = null;
        for (BookCatalog bc : pageInfo.getList()) {
            info = new BookInfo();
            info.setId(bc.getBookInfoId());
            BookInfo bi = bookInfoService.get(info);
            if (bi != null) {
                bc.setBookName(bi.getBookName());
            }
        }
        return pageInfo;
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(BookCatalog bookCatalog) {

        // 先检索目录 然后创建索引
        BookCatalog catalog = new BookCatalog();
        catalog.setBookInfoId(bookCatalog.getBookInfoId());
        BookCatalog bc = bookCatalogService.selectOrderByIndexDesc(catalog);
        if (bc == null) {
            bookCatalog.setCatalogIndex(1);
        } else {
            bookCatalog.setCatalogIndex(bc.getCatalogIndex() + 1);
        }
        bookCatalogService.save(bookCatalog);
        return Result.success("保存成功");
    }

    @RequestMapping(value = "/logicalDeletion", method = RequestMethod.POST)
    @ResponseBody
    public Result logicalDeletion(@RequestParam("id") String id) {
        bookCatalogService.logicalDeletion(id);
        return Result.success();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestParam("ids") String[] ids) {
        bookCatalogService.delete(ids);
        return Result.success();
    }

}